package com.example.hotel;

import java.net.URL;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.os.Bundle;
import android.os.StrictMode;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Typeface;
import android.telephony.TelephonyManager;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {
	Button bt_thai, bt_english;
	URL url = null;
	String device_id = "";
	TextView namecustomer, tv_th, tv_en;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		((Global) getApplicationContext()).id_imageInfo = "";
		((Global) getApplicationContext()).lang = "";
		((Global) getApplicationContext()).imei = "";
		if (android.os.Build.VERSION.SDK_INT > 9) {
			StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
					.permitAll().build();
			StrictMode.setThreadPolicy(policy);
		}
		namecustomer = (TextView) findViewById(R.id.nameCustomer);
		getName();
		getCount_cart();

		bt_thai = (Button) findViewById(R.id.th);
		bt_english = (Button) findViewById(R.id.en);
		bt_thai.setOnClickListener(this);
		bt_english.setOnClickListener(this);

		/*-----SetFonts-----*/
		tv_th = (TextView) findViewById(R.id.tv_th);
		tv_en = (TextView) findViewById(R.id.tv_en);
		Typeface Font = Typeface.createFromAsset(getAssets(),
				"fonts/PSL-Kittithada.ttf");
		Typeface FontBold = Typeface.createFromAsset(getAssets(),
				"fonts/PSL-KittithadaBold.ttf");
		namecustomer.setTypeface(FontBold);
		tv_th.setTypeface(Font);
		tv_en.setTypeface(Font);
		/*------------------*/

	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.th:
			((Global) getApplicationContext()).lang = "th";
			startActivity(new Intent(MainActivity.this, show_keyboard.class));
			break;
		case R.id.en:
			((Global) getApplicationContext()).lang = "en";
			startActivity(new Intent(MainActivity.this, show_keyboard.class));
			break;
		}
	}

	public void getName() {
		String URL1 = ((Global) getApplicationContext()).url;
		String SOAP_ACTION = ((Global) getApplicationContext()).soap_acction
				+ "home";
		String NAMESPACE = "";
		String METHOD_NAME = "home";
		TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
		device_id = tm.getDeviceId();
		//device_id = "410412150911850";
		((Global) getApplicationContext()).imei = device_id;
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
		SoapObject responses = null;
		request.addProperty("id", device_id);
		request.addProperty("lang", "en");
		// request.addProperty("room","0001");
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);
		HttpTransportSE androidHttpTransport = new HttpTransportSE(URL1);
		androidHttpTransport.debug = true;

		try {
			androidHttpTransport.call(SOAP_ACTION, envelope);
			responses = (SoapObject) envelope.bodyIn;
			String str = responses.toString();
			int indexSub = str.indexOf("=");
			String b = str.substring(indexSub + 1);
			indexSub = b.indexOf(";");
			b = b.substring(0, indexSub);
			String[] a = b.split("\\|");
			namecustomer.setText(a[0] + "\n" + a[1]);

		} catch (Exception e) {
			// Toast.makeText(getApplicationContext(),"Not connect internet",
			// Toast.LENGTH_LONG).show();
			AlertDialog.Builder builder = new AlertDialog.Builder(
					MainActivity.this);
			builder.setTitle("Alert Dailog")
					.setIcon(getResources().getDrawable(R.drawable.ic_launcher))
					.setMessage("Not connect internet")
					.setPositiveButton("Close",
							new DialogInterface.OnClickListener() {

								public void onClick(DialogInterface dialog,
										int which) {
									//MainActivity.this.finish();
									startActivity(new Intent(MainActivity.this,MainActivity.class));
								}
							});
			AlertDialog alert = builder.create();
			alert.show();
		}
	}

	public void getCount_cart() {
		String URL1 = ((Global) getApplicationContext()).url;
		String SOAP_ACTION = ((Global) getApplicationContext()).soap_acction
				+ "show_cart";
		String NAMESPACE = "";
		String METHOD_NAME = "show_cart";
		TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
		// device_id = tm.getDeviceId();
		//device_id = "410412150911850";
		((Global) getApplicationContext()).imei = device_id;
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
		SoapObject responses = null;
		request.addProperty("id", device_id);
		request.addProperty("lang", "en");
		// request.addProperty("room","0001");
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);
		HttpTransportSE androidHttpTransport = new HttpTransportSE(URL1);
		androidHttpTransport.debug = true;

		try {
			androidHttpTransport.call(SOAP_ACTION, envelope);
			responses = (SoapObject) envelope.bodyIn;
			String str = responses.toString();
			int indexSub = str.indexOf("=");
			String b = str.substring(indexSub + 1);
			indexSub = b.indexOf(";");
			b = b.substring(0, indexSub);
			String[] a = b.split("\\|");
			if (a[0].equals("")) {
				((Global) getApplicationContext()).cart_count = 0;

			} else {
				((Global) getApplicationContext()).cart_count = a.length;
			}

		} catch (Exception e) {
			// Toast.makeText(getApplicationContext(),"Not connect internet",
			// Toast.LENGTH_LONG).show();
			AlertDialog.Builder builder = new AlertDialog.Builder(
					MainActivity.this);
			builder.setTitle("Alert Dailog")
					.setIcon(getResources().getDrawable(R.drawable.logo))
					.setMessage("Not connect internet")
					.setPositiveButton("Close",
							new DialogInterface.OnClickListener() {

								public void onClick(DialogInterface dialog,
										int which) {
									MainActivity.this.finish();
								}
							});
			AlertDialog alert = builder.create();
			alert.show();
		}
	}

	/*
	 * public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode ==
	 * KeyEvent.KEYCODE_1 && event.getRepeatCount() == 0) {
	 * event.startTracking(); return true; } return super.onKeyDown(keyCode,
	 * event); }
	 * 
	 * public boolean onKeyUp(int keyCode, KeyEvent event) { if (keyCode ==
	 * KeyEvent.KEYCODE_1 && event.isTracking() && !event.isCanceled()) { // ***
	 * DO ACTION HERE *** startActivity(new Intent(MainActivity.this,
	 * Media.class)); //Toast.makeText(MainActivity.this,"1"
	 * ,Toast.LENGTH_LONG).show(); //String ch9 =
	 * "rtsp://203.151.21.9:1935/live/Saranair"; //vdoplay(ch9);
	 * 
	 * return true; } return super.onKeyUp(keyCode, event); }
	 */
}
